package com.jzlin.funhub.admin.controller;

import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jzlin.funhub.admin.model.dto.*;
import com.jzlin.funhub.admin.model.req.*;
import com.jzlin.funhub.admin.model.resp.ArticleDetailResp;
import com.jzlin.funhub.admin.service.article.*;
import com.jzlin.funhub.framework.core.http.RestRet;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 博客文章 前端控制器
 * </p>
 *
 * @author jzlin
 * @since 2025-07-15
 */
@RestController
@Api(tags = {"博客文章"})
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    @Autowired
    private ArticleCategoryService articleCategoryService;
    @Autowired
    private ArticleStarService articleStarService;
    @Autowired
    private ArticleCommentService articleCommentService;
    @Autowired
    private ArticleCommentRepliesService articleCommentRepliesService;
    @Autowired
    private ArticleTagsRefService articleTagsRefService;

    @ApiOperation("分页查询文章列表")
    @PostMapping("/page")
    public RestRet<IPage<ArticlePageDTO>> pageArticle(@RequestBody @Valid ArticlePageReq req) {
        return RestRet.createSuccessRet(articleService.queryPage(req));
    }

    @ApiOperation("查询最近的文章列表（5条）")
    @PostMapping("/recent")
    public RestRet<List<ArticleRecentDTO>> queryRecent(@RequestBody ArticleRecentReq req) {
        return RestRet.createSuccessRet(articleService.queryRecent(req));
    }

    @ApiOperation("查询文章分类树")
    @GetMapping("/category/tree")
    public RestRet<List<Tree<Long>>> queryCategoryTree() {
        return RestRet.createSuccessRet(articleCategoryService.queryTree());
    }

    @ApiOperation("列出所有分类下的文章数量")
    @PostMapping("/category/count")
    public RestRet<List<ArticleCategoryGroupDTO>> countArticleByCategory(@RequestBody ArticleCategoryCountReq req) {
        return RestRet.createSuccessRet(articleCategoryService.countArticleByCategory(req));
    }

    @ApiOperation("获取文章详情")
    @GetMapping("/detail")
    public RestRet<ArticleDetailResp> getDetail(Long id) {
        return RestRet.createSuccessRet(articleService.getDetail(id));
    }

    @ApiOperation("点赞/取消点赞文章")
    @PostMapping("/star")
    public RestRet<Void> starArticle(@RequestBody @Valid ArticleStarReq req) {
        articleStarService.starArticle(req);
        return RestRet.createSuccessRet();
    }

    @ApiOperation("分页查询评论")
    @PostMapping("/comment/page")
    public RestRet<Page<ArticleCommentPageDTO>> pageComment(@RequestBody @Valid ArticleCommentPageReq req) {
        return RestRet.createSuccessRet(articleCommentService.queryPage(req));
    }

    @ApiOperation("发表文章评论")
    @PostMapping("/comment/publish")
    public RestRet<Void> publishComment(@RequestBody @Valid ArticleCommentPublishReq req) {
        articleCommentService.publishArticleComment(req);
        return RestRet.createSuccessRet();
    }

    @ApiOperation("保存文章草稿")
    @PostMapping("/saveDraft")
    public RestRet<Long> saveDraftQuestion(@RequestBody @Valid ArticleSaveDraftReq req) {
        return RestRet.createSuccessRet(articleService.saveDraft(req));
    }

    @ApiOperation("发布新文章")
    @PostMapping("/publish")
    public RestRet<Long> publishNewArticle(@RequestBody @Valid ArticlePublishReq req) {
        return RestRet.createSuccessRet(articleService.publishNewArticle(req));
    }

    @ApiOperation("发表文章评论回复")
    @PostMapping("/comment/reply/publish")
    public RestRet<Long> publishCommentReply(@RequestBody @Valid ArticleCommentReplyPublishReq req) {
        return RestRet.createSuccessRet(articleCommentRepliesService.publishCommentReply(req));
    }

    @ApiOperation("列出所有标签和文章数量")
    @GetMapping("/tag/count")
    public RestRet<List<ArticleTagGroupDTO>> countArticleByTag() {
        return RestRet.createSuccessRet(articleTagsRefService.countArticleByTag());
    }


}
